/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import db.ConnectionFactory;
import entity.AccountEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author nhung_000
 */
public class AccountDAL extends BaseController<AccountEntity> {

    @Override
    public List<AccountEntity> getAll() throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public AccountEntity getById(AccountEntity t) throws Exception {
        open();
        AccountEntity e = null;
        String[] param = {t.getId() + ""};
        ResultSet rs = select("SELECT * FROM account WHERE id = ?", param);
        if (rs.next()) {
            e = new AccountEntity(rs.getInt("id"), rs.getString("username"), rs.getString("password"),
                    rs.getString("fullname"), rs.getString("email"));
        }
        close();
        return e;
    }

    public AccountEntity checkLogin(AccountEntity t) throws Exception {
        open();
        AccountEntity e = null;
        String[] param = {t.getUsername()};
        ResultSet rs = select("SELECT * FROM account WHERE username = ?", param);
        if (rs.next()) {
            e = new AccountEntity(rs.getInt("id"), rs.getString("username"), rs.getString("password"),
                    rs.getString("fullname"), rs.getString("email"));
        }
        close();
        return e;
    }

    @Override
    public int add(AccountEntity t) throws Exception {
        open();
        Date aDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Calendar cal = Calendar.getInstance();
        String formattedDate = formatter.format(cal.getTime());
        String[] param = {t.getUsername(), t.getPassword(), t.getFullname(), t.getEmail() + ""};
        int run = run("INSERT INTO account(username,password,fullname,email)"
                + "VALUES (?,?,?,?)", param);
        close();
        return run;
    }

    @Override
    public int update(AccountEntity t) throws Exception {
        open();
        Date aDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Calendar cal = Calendar.getInstance();
        String formattedDate = formatter.format(cal.getTime());
        Connection conn = ConnectionFactory.createConnection();
        PreparedStatement pstmt = conn.prepareStatement("UPDATE account SET username = ?,password = ?,fullname = ?,email = ?"
                + " WHERE id = ?");
        pstmt.setString(1, t.getUsername());
        pstmt.setString(2, t.getPassword());
        pstmt.setString(3, t.getFullname());
        pstmt.setString(4, t.getEmail());
        pstmt.setInt(1, t.getId());
        return pstmt.executeUpdate();

    }

    @Override
    public int delete(AccountEntity t) throws Exception {
        open();
        String[] param = {t.getId() + ""};
         Connection conn = ConnectionFactory.createConnection();
         PreparedStatement pstmt = conn.prepareStatement( "DELETE FROM account WHERE id = ?");
         pstmt.setInt(1, t.getId());
      
      return pstmt.executeUpdate();
    }

}
